#!/usr/bin/env python

# (c) 2007-2011 Helsinki University of Technology and University of Helsinki
# Licensed under the MIT license; see LICENSE.txt for more information.

from harava.document import Document
from harava.rdf import *
from rdf import RDFImporter

import re
import sys
import socket
from cStringIO import StringIO

class MicroformatImporter (RDFImporter):
  def __init__(self, session):
    RDFImporter.__init__(self, session)

  def process(self, rdfdoc):
    # open connection to swignitiond socket
    # TODO configurable host/port
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
      sock.connect(("localhost", 26464))
    except socket.error:
      # if connection fails, abort
      print >>sys.stderr, "Connection to Swignition daemon failed."
      return False

    sock.send("SET FORMAT turtle\r\n")
    sock.send("SET AUTOCLOSE 1\r\n")
    
    # send data (STDIN option)
#    sock.send("COGNIFY STDIN AS %s\r\n" % rdfdoc.getURL())
    
#    print "Sending to Swignition:"
#    print rdfdoc.getData()

#    sock.send("%s\r\n.\r\n" % rdfdoc.getData())

    # send URL
    sock.send("COGNIFY %s\r\n" % rdfdoc.getURL())
    
    # read result
    rdfdata = StringIO()
    while True:
      data = sock.recv(4096)
      if not data: break

      rdfdata.write(data)
    
    # close socket from our side
    sock.close()

    # go to beginning of data stream
    rdfdata.seek(0)

    # dirty fix for Swignition bug: remove language tag if a datatype is present
    # otherwise rdflib will be unable to parse the N3 file
    rdfdata = StringIO(re.sub(r'@[-a-zA-Z_]+\^\^', '^^', rdfdata.read()))

#    print "Data after sanitizing lang/datatype:"
#    print rdfdata.read()
#    rdfdata.seek(0)

    # parse rdfdata into self._rdf
    rdf = getGraph()
    success = True
    try:
      rdf.parse(rdfdata, format='n3')
    except:
      success = False

    rdfdata.close()
    
    if success:
      self._rdf += rdf
    else:
      print >>sys.stderr, "RDF parsing for Swignition-processed %s failed." % rdfdoc
    
    return success
  
  # NOTE: the RDFImporter.finish() method is used to process the parsed RDF
    
